package data
{
	import flash.utils.ByteArray;
	
	import array.NsArray;
	import array.NsSetArray;
	import array.StringArray;
	
	import kinds.MultinameKind;
	
	import util.BytesUtil;

	public class Multiname
	{
		private var _strArr:StringArray = null;
		private var _nsArr:NsArray = null;
		private var _nssetArr:NsSetArray = null;
		public var kind:uint = 0;
		public var nsIdx:uint = 0;
		public var nameIdx:uint = 0;
		public function Multiname(strArr:StringArray, nsArr:NsArray, nssetArr:NsSetArray)
		{
			this._strArr = strArr;
			this._nsArr = nsArr;
			this._nssetArr = nssetArr;
		}
		
		public function fill(kind:uint, bytes:ByteArray):void
		{
			var ret:Object = null;
			switch(kind){
				case MultinameKind.CONSTANT_QName:
				case MultinameKind.CONSTANT_QNameA:
					this.kind = kind;
					this.nsIdx = BytesUtil.readU30(bytes);
					this.nameIdx = BytesUtil.readU30(bytes);
					break;
				
				case MultinameKind.CONSTANT_RTQName:
				case MultinameKind.CONSTANT_RTQNameA:
					this.kind = kind;
					this.nameIdx = BytesUtil.readU30(bytes);
					break;
				
				case MultinameKind.CONSTANT_RTQNameL:
				case MultinameKind.CONSTANT_RTQNameLA:
					this.kind = kind;
					break;
				case MultinameKind.CONSTANT_Multiname:
				case MultinameKind.CONSTANT_MultinameA:
					this.kind = kind;
					this.nameIdx = BytesUtil.readU30(bytes);
					this.nsIdx = BytesUtil.readU30(bytes);
					break;
				case MultinameKind.CONSTANT_MultinameL:
				case MultinameKind.CONSTANT_MultinameLA:
					this.kind = kind;
					this.nsIdx = BytesUtil.readU30(bytes);
					break;
			}
		}
		
		public function toString():String
		{
			var str:String = "";
			var nsSet:Array = null;
			var len:int = 0;
			var i:int = 0;
			switch(kind){
				case MultinameKind.CONSTANT_QName:
				case MultinameKind.CONSTANT_QNameA:
					str = _nsArr[this.nsIdx].name + ":" + _strArr[this.nameIdx];
					break;
				
				case MultinameKind.CONSTANT_RTQName:
				case MultinameKind.CONSTANT_RTQNameA:
					str = _strArr[this.nameIdx];
					break;
				
				case MultinameKind.CONSTANT_RTQNameL:
				case MultinameKind.CONSTANT_RTQNameLA:
					str = "RTQNameL";
					break;
				case MultinameKind.CONSTANT_Multiname:
				case MultinameKind.CONSTANT_MultinameA:
					nsSet =_nssetArr[this.nsIdx];
					len = nsSet.length;
					for (i = 0; i < len - 1; i++){
						str += nsSet[i] + ",";
					}
					if (len > 0){
						str +=nsSet[len - 1] + ":";
					}
					str += _strArr[this.nameIdx];
					break;
				case MultinameKind.CONSTANT_MultinameL:
				case MultinameKind.CONSTANT_MultinameLA:
					nsSet =_nssetArr[this.nsIdx];
					len = nsSet.length;
					for (i = 0; i < len - 1; i++){
						str += nsSet[i] + ",";
					}
					if (len > 0){
						str +=nsSet[len - 1] + ":";
					}
					str += _strArr[this.nameIdx];
					break;
			}
			return str;
		}
	}
}